package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizGatewayTemperature;
import cn.stylefeng.guns.modular.gridsystem.enums.LogExceptionEnum;
import cn.stylefeng.guns.modular.gridsystem.enums.SystemParamExceptionEnum;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizGatewayTemperatureMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizGatewayTemperatureParam;
import cn.stylefeng.guns.modular.gridsystem.model.result.BizGatewayTemperatureResult;
import cn.stylefeng.guns.modular.gridsystem.service.BizGatewayTemperatureService;
import cn.stylefeng.guns.modular.gridsystem.utils.OptionalBean;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 网关温度 服务实现类
 *
 * @author ssy
 * @date 2022/08/12 16:00
 */
@Service
public class BizGatewayTemperatureServiceImpl extends ServiceImpl<BizGatewayTemperatureMapper, BizGatewayTemperature> implements BizGatewayTemperatureService {

    /**
     * 分页查询网关温度
     *
     * @param bizGatewayTemperatureParam
     * @return
     */
    @Override
    public PageResult<Map<String, Object>> pageList(BizGatewayTemperatureParam bizGatewayTemperatureParam) {
        Page<Map<String, Object>> mapPage = PageFactory.defaultPage();
        String gatewayIds = bizGatewayTemperatureParam.getGatewayIds();
        if (StringUtils.isNotBlank(gatewayIds)) {
            final String[] split = gatewayIds.split(",");
            mapPage = baseMapper.pageList(PageFactory.defaultPage(), bizGatewayTemperatureParam, split);
        }
        // 查询分页结果
        return new PageResult<>(mapPage);
    }

    /**
     * 添加网关温度
     *
     * @param bizGatewayTemperatureParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BizGatewayTemperatureParam bizGatewayTemperatureParam) {
        OptionalBean.ofNullable(bizGatewayTemperatureParam).orElseThrow(() -> new ServiceException(SystemParamExceptionEnum.PARAM_NULL_ERROR));
        // 将dto转为实体
        BizGatewayTemperature bizGatewayTemperature = new BizGatewayTemperature();
        BeanUtil.copyProperties(bizGatewayTemperatureParam, bizGatewayTemperature);
        bizGatewayTemperature.setCreateUser(LoginContextHolder.me().getSysLoginUser().getId());
        bizGatewayTemperature.setCreateTime(new Date());
        this.save(bizGatewayTemperature);
    }

    /**
     * 删除网关温度
     *
     * @param bizGatewayTemperatureParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizGatewayTemperatureParam bizGatewayTemperatureParam) {
        removeById(bizGatewayTemperatureParam.getId());
    }

    /**
     * 编辑网关温度
     *
     * @param bizGatewayTemperatureParam
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(BizGatewayTemperatureParam bizGatewayTemperatureParam) {
        OptionalBean.ofNullable(bizGatewayTemperatureParam).orElseThrow(() -> new ServiceException(SystemParamExceptionEnum.PARAM_NULL_ERROR));
        // 根据id查询实体
        BizGatewayTemperature bizGatewayTemperature = new BizGatewayTemperature();
        // 请求参数转化为实体
        BeanUtil.copyProperties(bizGatewayTemperatureParam, bizGatewayTemperature);
        bizGatewayTemperature.setUpdateUser(LoginContextHolder.me().getSysLoginUser().getId());
        bizGatewayTemperature.setUpdateTime(new Date());
        updateById(bizGatewayTemperature);
    }

    /**
     * 查看详情网关温度
     *
     * @param bizGatewayTemperatureParam
     * @return
     */
    @Override
    public BizGatewayTemperatureResult detail(BizGatewayTemperatureParam bizGatewayTemperatureParam) {
        return queryBizGatewayTemperature(bizGatewayTemperatureParam);
    }

    /**
     * 查询在线网关id
     *
     * @return
     */
    @Override
    public List<Integer> listByBeforeTime() {
        return baseMapper.listByBeforeTime();
    }


    /**
     * 获取网关温度
     *
     * @author ssy
     * @since 2022/08/12 16:00
     */
    private BizGatewayTemperatureResult queryBizGatewayTemperature(BizGatewayTemperatureParam bizGatewayTemperatureParam) {
        OptionalBean.ofNullable(bizGatewayTemperatureParam).orElseThrow(() -> new ServiceException(SystemParamExceptionEnum.PARAM_NULL_ERROR));
        OptionalBean.ofStr(bizGatewayTemperatureParam.getId());
        BizGatewayTemperature gatewayTemperature = getById(bizGatewayTemperatureParam.getId());
        OptionalBean.ofNullable(gatewayTemperature).orElseThrow(() -> new ServiceException(LogExceptionEnum.NOT_EXISTED));
        BizGatewayTemperatureResult bizChannelTypeResult = new BizGatewayTemperatureResult();
        BeanUtil.copyProperties(gatewayTemperature, bizChannelTypeResult);
        return bizChannelTypeResult;
    }

    /**
     * echart查询时间段的温度
     *
     * @author rdp
     * @date 2022/11/02 09:58
     */
    @Override
    public List<BizGatewayTemperature> getTemperatureByDate(BizGatewayTemperatureParam bizGatewayTemperatureParam) {
        List<BizGatewayTemperature> temperatureList = baseMapper.getTemperatureByDate(bizGatewayTemperatureParam);
        Iterator<BizGatewayTemperature> iterator = temperatureList.iterator();
        int ctn = 0;
        while (iterator.hasNext()) {
            if (ctn++ % 4 != 0) {
                iterator.remove();
            }
            iterator.next();
        }
        return temperatureList;
    }
}
